package com.yida.gwj.config.controller;

import com.yida.common.core.domain.R;
import com.yida.common.core.enums.UserStatus;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.api.domain.GwjConfStepConfigInfo;
import com.yida.gwj.config.domain.dto.GwjConfStepConfigInfoDTO;
import com.yida.gwj.config.service.IGwjConfStepConfigInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务步骤配置信息Controller
 * <p>
 * 修订20230112 hmh(修改地址，方法名替换，重复对象构建)
 *
 * @author lqq
 * @date 2022-12-07
 */
@RestController
@RequestMapping("/stepConfig")
public class GwjConfStepConfigInfoController extends BaseController {

	@Autowired
	private IGwjConfStepConfigInfoService stepConfigInfoService;

	/**
	 * 获取步骤所有配置
	 */
	@GetMapping("/info")
	public AjaxResult stepConfig() {
		List<GwjConfStepConfigInfo> infos = stepConfigInfoService.selectGwjConfStepConfigInfoList(new GwjConfStepConfigInfo());
		return AjaxResult.success(infos.stream().map(GwjConfStepConfigInfo::getCode).collect(Collectors.toList()));
	}

	/**
	 * 查询业务步骤配置信息列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("gwj:config:stepConfig:list")
	public TableDataInfo list(GwjConfStepConfigInfo gcStepConfigInfo) {
		startPage();
		List<GwjConfStepConfigInfo> list = stepConfigInfoService.selectGwjConfStepConfigInfoList(gcStepConfigInfo);
		return GwjConvertUtil.convertTable(getDataTable(list), GwjConfStepConfigInfoDTO.class);
	}

	/**
	 * 查询业务步骤列表
	 */
	@GetMapping("/list/allValid")
	@RequiresPermissions("gwj:config:stepConfig:list")
	public AjaxResult listAllValid() {
		GwjConfStepConfigInfo gcStepConfigInfo = new GwjConfStepConfigInfo();
		gcStepConfigInfo.setStatus(UserStatus.OK.getCode());
		List<GwjConfStepConfigInfo> list = stepConfigInfoService.selectGwjConfStepConfigInfoList(gcStepConfigInfo);
		if (StringUtils.isEmpty(list)) {
			return AjaxResult.success(new ArrayList<>());
		}
		return AjaxResult.success(GwjConvertUtil.convertList(list, GwjConfStepConfigInfoDTO.class));
	}

	/**
	 * 导出业务步骤配置信息列表
	 */
	@PostMapping("/export")
	@RequiresPermissions("gwj:config:stepConfig:export")
	@Log(title = "业务步骤配置信息", businessType = BusinessType.EXPORT, systemName = "gwj")
	public void export(HttpServletResponse response, GwjConfStepConfigInfo gcStepConfigInfo) {
		List<GwjConfStepConfigInfo> list = stepConfigInfoService.selectGwjConfStepConfigInfoList(gcStepConfigInfo);
		ExcelUtil<GwjConfStepConfigInfo> util = new ExcelUtil<GwjConfStepConfigInfo>(GwjConfStepConfigInfo.class);
		util.exportExcel(response, list, "业务步骤配置信息数据");
	}

	/**
	 * 获取业务步骤配置信息详细信息
	 */
	@GetMapping(value = "/{configId}")
	@RequiresPermissions("gwj:config:stepConfig:query")
	public AjaxResult getInfo(@PathVariable("configId") Long configId) {
		return AjaxResult.success(stepConfigInfoService.selectGwjConfStepConfigInfoByConfigId(configId));
	}

	/**
	 * 新增业务步骤配置信息
	 */
	@PostMapping
	@RequiresPermissions("gwj:config:stepConfig:add")
	@Log(title = "业务步骤配置信息", businessType = BusinessType.INSERT, systemName = "gwj")
	public AjaxResult add(@Validated(GwjConfStepConfigInfo.Save.class) @RequestBody GwjConfStepConfigInfo gcStepConfigInfo) {
		return toAjax(stepConfigInfoService.insertGwjConfStepConfigInfo(gcStepConfigInfo));
	}

	/**
	 * 修改业务步骤配置信息
	 */
	@PutMapping
	@RequiresPermissions("gwj:config:stepConfig:edit")
	@Log(title = "业务步骤配置信息", businessType = BusinessType.UPDATE, systemName = "gwj")
	public AjaxResult edit(@Validated(GwjConfStepConfigInfo.Update.class) @RequestBody GwjConfStepConfigInfo gcStepConfigInfo) {
		return toAjax(stepConfigInfoService.updateGwjConfStepConfigInfo(gcStepConfigInfo));
	}

	/**
	 * 查询业务步骤配置信息
	 */
	@InnerAuth
	@GetMapping("/inner/codeList/{stepId}")
	public R<List<String>> selectGwjConfStepConfigInfoCodeListByStepId(@PathVariable("stepId") Long stepId) {
		return R.ok(stepConfigInfoService.selectGwjConfStepConfigInfoCodeListByStepId(stepId));
	}
}
